using namespace std;
#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>

class Solution
{
public:
    string minWindow(string s, string t)
    {
        string rs;
        unordered_map<char, int> hashs;
        unordered_map<char, int> hasht;
        for(auto e : t)
        {
            hasht[e]++;
        }
        int len = INT_MAX;
        int left = 0, right = 0, count = 0;
        int m = s.size(), n = hasht.size();//不能是t.size() "aa"这种count=1，而t.size()=2
        int rsleft = 0;
        while(right < m)
        {
            char in = s[right];
            if(++hashs[in] == hasht[in])
                count++;

            while(count == n)
            {
                if(right-left+1 < len)
                {
                    len = right-left+1;
                    rsleft = left;
                }
                char out = s[left++];
                if(hashs[out]-- == hasht[out])
                {
                    count--;
                }
            }
            right++;
        }
        if(len == INT_MAX)
            return "";
        else
            return s.substr(rsleft, len);
    }
};